<?php

class DataManager {

    public static function userInfo($data = array()) {
        $new_data = array("id" => $_SESSION['id'], "secret" => $_SESSION['secret']);
        return array_merge($data, $new_data);
    }

    public static function inLoginData($data = array()) {
        $data = DataManager::allUsers($data);
        return $data;
    }

    public static function getRoomCardsData($room) {
        return $data = $room->getRoomCardsData();
    }

    public static function lobbyData($lobby, $data = array()) {
        $data['cards'] = $lobby->lobbyData($data);
        return $data;
    }

    public static function inLobbyData($lobby) {
        $data = array();
        $userid = getUserId();
        $data = DataManager::lobbyData($lobby);
        $data = DataManager::userInfo($data);
        $data = DataManager::lobbyUsers($data);
        $data = DataManager::chat($lobby, $data);
        //$data = DataManager::screenMessages($lobby, null, $data);
        $data = DataManager::getInputData($lobby, $userid, $data);
        $data = DataManager::uniqueID($userid, $data);
        $data = DataManager::alertPlayer($userid, $data);
        return $data;
    }

    public static function inGameData($game, $data = array()) {
        $userid = getUserId();

        $data = DataManager::userInfo($data);
        $data = DataManager::gameInfo($game, $data);
        $data = DataManager::resources($game, $data);
        $data = DataManager::playerData($game, $data);
        $data = DataManager::boardData($game, $data);
        $data = DataManager::chat($game, $data);
        $data = DataManager::screenMessages($game, $userid, $data);
        $data = DataManager::getInputData($game, $userid, $data);
        $data = DataManager::uniqueID($userid, $data);
        $data = DataManager::alertPlayer($userid, $data);
        return $data;
    }

    public static function chat($room, $data = array()) {
        $data = Chat::getChatData($room, $data);
        return $data;
    }

    private static function screenMessages($room, $name, $data) {
        $data = ScreenDisplay::getScreenData($room, $name, $data);
        return $data;
    }

    private static function gameInfo(Game $game, $data = array()) {

        $game_data = $game->getData();
        return array_merge($data, $game_data);
    }

    public static function resources(Game $game, $data = array()) {

         $name = getUserId();
        $data['public'] = $game->getPublicPlayersData();
        $data['resources'] = $game->getPublicPlayerData($name);
        return $data;
    }

    public static function playerData(Game $game, $data = array()) {

        $name = getUserId();

        $data = $game->getPlayerData($data, $name);
        $phase = $data['player']['phase'];

        if ($phase == Phase_Buy || $phase == Phase_Payment) {
            $data = DataManager::getBuyData($game, $data);
        }
        return $data;
    }

    public static function uniqueId($name, $data = array()) {
        $db = DBUtils::Create();
        $data['uniqueid'] = $db->getUniqueID($name);
        return $data;
    }
    
    public static function getChatLinesIDs($room) {
        $db = DBUtils::Create();
        $data = Chat::getChatIDs($room);
        return $data;
    }

    public static function alertPlayer($name, $data = array()) {
        $db = DBUtils::Create();
        $data['alertPlayer'] = $db->getAlert($name);
        return $data;
    }

    public static function boardData(Game $game, $data = array()) {
        $data = $game->getBoardData($data);
        return $data;
    }

    private static function getPaymentData($game, $data = array()) {
        $data['buy']['cards'] = $game->getBuyCards();
        return $data;
    }

    private static function getBuyData($game, $data = array()) {
        $data['buy']['cards'] = $game->getBuyCards();
        return $data;
    }

    public static function getUniqueID($userid, $data = array()) {
        $db = DBUtils::Create();
        $uniqueid = $db->GetUniqueID($userid);
        return $uniqueid;
    }

    public static function getRoomId($userid, $data = array()) {
        $db = DBUtils::Create();
        $roomid = $db->getAllRoomsForPlayer($userid);
        $data['roomid'] = $roomid[0][0];
        return $data;
    }

    public static function getInputData($room, $name, $data = array()) {

        $db = DBUtils::Create();
        $input_arr = array();
        $input = null;
        if (!is_object($room)) {
            if ($room > 0)
                $room = loadgame();
            else
                $room = loadLobby();
        }
        $input = MySocket::getInput($room, $name);
        $data['input'] = array();
        $params = preg_split('/\|/', $input['params'], -1, PREG_SPLIT_NO_EMPTY);
        $data['input']['params'] = $params;
        $data['input']['min'] = $input['min'];
        $data['input']['max'] = $input['max'];
        $data['input']['extraparams'] = $input['extraparams'];
        $data['input']['screen'] = $input['screen'];
        $data['input']['type'] = $input['inputtype'];

        return $data;
    }

    private static function allGames($data = array()) {
        $db = DBUtils::Create();
        $data['allGames'] = $db->getAllGames();
        return $data;
    }

    private static function lobbyUsers($data = array()) {
        $db = DBUtils::Create();
        $data['users'] = $db->getLobbyUsers();
        return $data;
    }

    private static function allUsers($data = array()) {
        $db = DBUtils::Create();
        $data['users'] = $db->getAllUsers();
        return $data;
    }

}

?>